package com.imti.mall.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imti.mall.model.order.pojo.OmsCartItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 购物车的添加，查询，修改的 Mapper
 */
@Mapper
public interface CartItemMapper extends BaseMapper<OmsCartItem> {
     /**
      * 添加
      * @param omsCartItem
      * @return
      */
     @Insert("insert into oms_cart_item(product_id,product_sku_id,member_id,quantity,price,product_pic,product_name,product_sub_title,product_sku_code,member_nickname,create_date,delete_status,product_category_id,product_brand,product_sn,product_attr) " +
             "values(#{productId},#{productSkuId},#{memberId},#{quantity},#{price},#{productPic},#{productName},#{productSubTitle},#{productSkuCode},#{memberNickname},now(),#{deleteStatus},#{productCategoryId},#{productBrand},#{productSn},#{productAttr})")
     int addCartItem(OmsCartItem omsCartItem);

     /**
      * 修改购物车内已拥有商品的数量
      * @param omsCartItem
      * @return
      */
     @Update("update oms_cart_item set quantity=quantity+#{quantity},modify_date=now() where member_id=#{memberId} and product_sku_id=#{productSkuId}")
     int updateCartItem(OmsCartItem omsCartItem);

     /**
      * 查询 用户购物车 未删除商品
      * 可以根据sku编码进行查询
      * 可以查询用户的所有的购物车信息
      * @param memberId 用户id
      * @param productSkuId Sku id
      * @return 返回结果为一条或多条购物车信息
      */
     @Select(" <script> " +
             " select product_id,product_sku_id,member_id,quantity,price,product_pic,product_name,product_sub_title,product_sku_code,member_nickname,create_date,modify_date,delete_status,product_category_id,product_brand,product_sn,product_attr " +
             " from oms_cart_item where member_id=#{memberId} " +
             " <if test=\" productSkuId != null and productSkuId != '' \" > and product_sku_id=#{productSkuId} </if> " +
             " and delete_status=0  " +
             " </script>")
     @Results({
             @Result(property = "productId",column = "product_id"),
             @Result(property = "productSkuId",column = "product_sku_id"),
             @Result(property = "memberId",column = "member_id"),
             @Result(property = "quantity",column = "quantity"),
             @Result(property = "price",column = "price"),
             @Result(property = "productPic",column = "product_pic"),
             @Result(property = "productName",column = "product_name"),
             @Result(property = "productSubTitle",column = "product_sub_title"),
             @Result(property = "productSkuCode",column = "product_sku_code"),
             @Result(property = "memberNickname",column = "member_nickname"),
             @Result(property = "createDate",column = "create_date"),
             @Result(property = "modifyDate",column = "modify_date"),
             @Result(property = "deleteStatus",column = "delete_status"),
             @Result(property = "productCategoryId",column = "product_category_id"),
             @Result(property = "productBrand",column = "product_brand"),
             @Result(property = "productSn",column = "product_sn"),
             @Result(property = "productAttr",column = "product_attr")
     })
     List<OmsCartItem> cartItemList(@Param("memberId") Long memberId, @Param("productSkuId") Long productSkuId);


     /**
      * 查询 用户购物车 未删除商品
      * 根据sku编码进行查询
      * @param memberId 用户id
      * @param productSkuId Sku id
      * @return 返回结果为一条
      */
     @Select(" select id,product_id,product_sku_id,member_id,quantity,price,product_pic,product_name,product_sub_title,product_sku_code,member_nickname,create_date,modify_date,delete_status,product_category_id,product_brand,product_sn,product_attr " +
             " from oms_cart_item where member_id=#{memberId} " +
             " and product_sku_id=#{productSkuId} " +
             " and delete_status=0  ")
     @Results({
             @Result(id=true,property = "id",column = "id"),
             @Result(property = "productId",column = "product_id"),
             @Result(property = "productSkuId",column = "product_sku_id"),
             @Result(property = "memberId",column = "member_id"),
             @Result(property = "quantity",column = "quantity"),
             @Result(property = "price",column = "price"),
             @Result(property = "productPic",column = "product_pic"),
             @Result(property = "productName",column = "product_name"),
             @Result(property = "productSubTitle",column = "product_sub_title"),
             @Result(property = "productSkuCode",column = "product_sku_code"),
             @Result(property = "memberNickname",column = "member_nickname"),
             @Result(property = "createDate",column = "create_date"),
             @Result(property = "modifyDate",column = "modify_date"),
             @Result(property = "deleteStatus",column = "delete_status"),
             @Result(property = "productCategoryId",column = "product_category_id"),
             @Result(property = "productBrand",column = "product_brand"),
             @Result(property = "productSn",column = "product_sn"),
             @Result(property = "productAttr",column = "product_attr")
     })
     OmsCartItem cartItem(@Param("memberId") Long memberId, @Param("productSkuId") Long productSkuId);

     @Select("SELECT COUNT(product_id) FROM `oms_cart_item` WHERE member_id=#{memberId} AND product_id=#{productId}")
     Integer quantity(@Param("memberId") Long memberId, @Param("productId") Long productId);

     @Update("update oms_cart_item set delete_status=1 where id=#{id}}")
     int updateDeleteStatus(Long id);
}
